CentOS 7
Sponsored Link

Salt : Use Salt State File#2
2016/10/08
 
This is the example to configure State Tree.
[1] Put top.sls called "Top File" under the root directory you defined.
[root@dlp ~]#
vi /srv/salt/top.sls
base:
  # define target Minions
  '*':
    # define the name of State file
    - default

# create State file defined in Top File

[root@dlp ~]#
vi /srv/salt/default.sls
# for example, Install and start httpd and MariaDB and also install PHP

webserver:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - php-mbstring
      - php-pear
      - mariadb-server

/var/www/html/index.php:
  file:
    - managed
    - source: salt://httpd/index.php
    - require:
      - pkg: webserver

# initial setup script
/tmp/setup.sql:
  file:
    - managed
    - source: salt://httpd/setup.sql

enable_httpd:
  service.running:
    - name: httpd
    - enable: True
    - require:
      - pkg: webserver

enable_mariadb:
  service.running:
    - name: mariadb
    - enable: True
    - require:
      - pkg: webserver

setup_mariadb:
  cmd.run:
    - name: '/bin/mysql -u root < /tmp/setup.sql'
    - require:
      - service: enable_mariadb

# if Firewalld is running, configure services
{% set fw_status = salt['service.status']('firewalld') %}
{% if fw_status %}
setup_fw:
  cmd.run:
    - names:
      - '/bin/firewall-cmd --add-service={http,https,mysql}'
      - '/bin/firewall-cmd --add-service={http,https,mysql} --permanent'
{% endif %}

# create index.php template

[root@dlp ~]#
vi /srv/salt/httpd/index.php
<?php
   print "Salt State Test Page";
?>

# create MariaDB initial setup script

[root@dlp ~]#
vi /srv/salt/httpd/setup.sql
set password for root@localhost=password('password');
set password for root@'127.0.0.1'=password('password'); 
delete from mysql.user where user='';
delete from mysql.user where password='';
drop database test;

# run state.apply ro apply settings

[root@dlp ~]#
salt "*" state.apply

node01.srv.world:
    ----------
    cmd_|-setup_fw_|-/bin/firewall-cmd --add-service={http,https,mysql} --permanent_|-run:
        ----------
        __run_num__:
            7
        changes:

.....
.....

 name:
            mariadb
        result:
            True
        start_time:
            19:56:52.911517

# verify

[root@dlp ~]#
salt "node01.srv.world" cmd.run 'systemctl status httpd'

[root@dlp ~]# salt "node02.srv.world" cmd.run 'systemctl status httpd'
node01.srv.world:
    * httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2016-10-17 10:56:52 JST; 2min 16s ago
.....
.....
[root@dlp ~]#
salt "node01.srv.world" cmd.run 'systemctl status httpd'

[root@dlp ~]# salt "node02.srv.world" cmd.run 'systemctl status httpd'
node01.srv.world:
    * mariadb.service - MariaDB database server
       Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2016-10-17 10:56:57 JST; 2min 24s ago
.....
.....
[root@dlp ~]#
salt "node01.srv.world" cmd.run 'mysql -u root -ppassword -e "show databases;"'

[root@dlp ~]# salt "node02.srv.world" cmd.run 'systemctl status httpd'
node01.srv.world:
    Database
    information_schema
    mysql
    performance_schema
[root@dlp ~]#
curl http://node01.srv.world/index.php

Salt State Test Page
 
Tweet